package org.jaaksi.customviewdemo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import org.jaaksi.customviewdemo.R;

/**
 * 刮刮乐
 */
public class GuagualeView extends View {
    private Paint mPaint;
    private Bitmap mSrcBmp;
    private Bitmap mDstBmp;
    private Path mPath;
    private PorterDuffXfermode xfermode;
    private Canvas dstCanvas;
    private float lastX;
    private float lastY;
    private Rect src;
    private Rect dst;

    public GuagualeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        //初始化画笔
        mPaint = new Paint();
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(80);
        mPaint.setTextSize(45);
        //禁用硬件加速，某些API不支持硬件加速
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        //初始化图像对象
        mSrcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.guaguale);
        // 指定原图的绘制区域（这里绘制整张图）。如只绘制宽度一半：mSrcBmp.getWidth()/2
        src = new Rect(0, 0, mSrcBmp.getWidth(), mSrcBmp.getHeight());
        //路径（贝赛尔曲线）
        mPath = new Path();
        xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mDstBmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        // 指定图片在屏幕上显示的区域，用于对图片进行缩放展示
        dstCanvas = new Canvas(mDstBmp);
        dst = new Rect(0, 0, w, h);
    }

    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                lastY = event.getY();
                mPath.moveTo(lastX, lastY);
                break;
            case MotionEvent.ACTION_MOVE:
                float endX = (event.getX() - lastX) / 2 + lastX;
                float endY = (event.getY() - lastY) / 2 + lastY;
                //画二阶贝塞尔曲线
                mPath.quadTo(lastX, lastY, endX, endY);
                lastX = event.getX();
                lastY = event.getY();
                //先将路径绘制到 bitmap上
                dstCanvas.drawPath(mPath, mPaint);
                break;
        }
        invalidate();
        return true;
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制刮奖结果
        mPaint.setStrokeWidth(1);
        canvas.drawText("抱歉，未中奖", getWidth() / 2f, getHeight() / 2f, mPaint);
        mPaint.setStrokeWidth(80);
        // 使用离屏绘制。这里要新建一个图层
        int layerID = canvas.saveLayer(0, 0, getWidth(), getHeight(), mPaint);
        //绘制源图像
        canvas.drawBitmap(mSrcBmp, src, dst, mPaint);
        //设置模式为DST_OUT,擦橡皮区域为交集区域需要清掉像素
        mPaint.setXfermode(xfermode);
        //绘制目标图像
        canvas.drawBitmap(mDstBmp, 0, 0, mPaint);
        mPaint.setXfermode(null);
        canvas.restoreToCount(layerID);
    }


}
